package test;

import java.util.ArrayList;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;
import org.jnetpcap.protocol.lan.Ethernet;
import org.jnetpcap.protocol.network.Ip4;

import packets.OSPF;

public class ospftest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	
	
		List<PcapIf> alldevs = new ArrayList<PcapIf>();
		StringBuilder errbuf = new StringBuilder(); 
		
		//List of all devices on this system
		
	int r = Pcap.findAllDevs(alldevs, errbuf);
	if (r == Pcap.NOT_OK || alldevs.isEmpty()){
		System.err.printf("Cant read list of devices, werror is %s", errbuf.toString());
		return;
	}
	
	System.out.println("Network devices found:");
	
	int i = 0;
	for(PcapIf device : alldevs){
		String description = (device.getDescription() != null) ? device.getDescription()
				: "No descritpion available";
		System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description);
	}
	
	PcapIf device = alldevs.get(4);
	System.out.printf("\nChoosing '%s' on your behalf:\n", (device.getDescription() != null) ? device.getDescription() : device.getName());
		
		int snaplen = 64*1024; //Alle Packete lesen, keine Beschränkung
		int flags = Pcap.MODE_PROMISCUOUS;
		int timeout = 15 * 1000;
	
		final OSPF ospf = new OSPF();
		Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
		
		PcapPacketHandler<String> packetHandler = new PcapPacketHandler<String>() {
			
			@Override
			public void nextPacket(PcapPacket packet, String user) {
				
			
				
				Ip4 ip = new Ip4();
				Ethernet ethernet = new Ethernet();
				if(packet.hasHeader(ip) && packet.hasHeader(ethernet)){
					System.out.println("IP gefunden und  Ethernet gefunden");
					System.out.println(packet.getHeader(ip).toString());
					 System.out.println(packet.getHeader(ip).getPacket().getHeader(ethernet).toString());
					
				}  
				
			}
		};
		
		pcap.loop(10, packetHandler, "works");
  		
  		
  		pcap.close();
		
	}

}
